package com.sp.scmdb.action;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.sp.scmdb.bean.AlarmTypeBean;
import com.sp.scmdb.common.CommonParamter;
import com.sp.scmdb.common.action.BaseAction;
import com.sp.scmdb.entity.AlarmType;
import com.sp.scmdb.service.AlarmService;

@Controller("alarmTypeAction")
@Scope("prototype")
public class AlarmTypeAction extends BaseAction<AlarmTypeBean> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private static Log log = LogFactory.getLog(AlarmTypeAction.class);
	private AlarmService alarmService;

	/**
	 * @param alarmService
	 *            the alarmService to set
	 */
	@Autowired
	public void setAlarmService(AlarmService alarmService) {
		this.alarmService = alarmService;
	}

	@SuppressWarnings("unchecked")
	public String findAll() {
		// 获取报警类型
		List<AlarmType> ListAT = alarmService.findAlarmType();
		session.put("ListAlarmType", ListAT);
		session.put("useType", CommonParamter.TypeCommon.getUseType());
		this.button("", "Super.editData('/SCMDB/alarmTypeAction!findType.action?id=',420,200)", "");
		return "success";
	}

	@SuppressWarnings("unchecked")
	public String findType() {
		try {
			String typeID = request.getParameter("id");
			AlarmTypeBean alarmTypeBean = alarmService.findAlarmTypeByID(typeID);
			session.put("useType", CommonParamter.TypeCommon.getUseType());
			if (null != alarmTypeBean) {
				setEntity(alarmTypeBean);
			} else {
				// 如果不存在，返回到查询页面
				return "commitSucc";
			}
		} catch (RuntimeException e) {
			log.error("findDep fail!reason:" + e.getMessage());
			return "fail";
		} catch (Exception e) {
			log.error("findDep fail!reason:" + e.getMessage());
			return "error";
		}
		return "editSucc";
	}

	public String editType() {
		try {
			String result = alarmService.editAlarmType(entity);
			if (log.isInfoEnabled()) {
				log.info("edit suncce");
			}
			messageSession(result, "报警类型");
		} catch (RuntimeException e) {
			log.error("edit alarmType fail!reason:" + e.getMessage());
			return "fail";
		} catch (Exception e) {
			log.error("edit alarmType fail!reason:" + e.getMessage());
			return "error";
		}
		return "commitSucc";
	}
}
